package com.qqtech.qquan.circle.service;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.qqtech.core.common.exception.BizzException;
import com.qqtech.core.common.util.StringUtil;
import com.qqtech.core.common.util.TimeUtil;
import com.qqtech.core.frame.dao.BaseDao;
import com.qqtech.core.frame.service.BaseServiceImpl;
import com.qqtech.qquan.circle.dao.CircleBaseDao;
import com.qqtech.qquan.circle.dao.CircleUpgradeApplyDao;
import com.qqtech.qquan.circle.enums.CircleUpgradeApplyStatusDictEnum;
import com.qqtech.qquan.circle.enums.CircleZoneLevelDictEnum;
import com.qqtech.qquan.circle.model.CircleBase;
import com.qqtech.qquan.circle.model.CircleUpgradeApply;

@Service
public class CircleUpgradeApplyServiceImpl extends BaseServiceImpl<CircleUpgradeApply>
		implements CircleUpgradeApplyService {
	public static final Logger log = LoggerFactory.getLogger(CircleUpgradeApplyServiceImpl.class);
	
	@Resource
	private CircleUpgradeApplyDao circleUpgradeApplyDao;
	
	@Resource
	private CircleBaseDao circleBaseDao;

	@Override
	protected BaseDao<CircleUpgradeApply> getBaseDao() {
		return circleUpgradeApplyDao;
	}

	@Override
	public int addApply(CircleUpgradeApply upgradeApply) throws BizzException{
		  if(upgradeApply.getApplyId()==null){
			  throw new BizzException("申请人为空.");
		  }
		  if(upgradeApply.getCircleId()==null){
			  throw new BizzException("圈子id为空.");
		  }
		  if(upgradeApply.getFromZoneLevel()==null){
			  throw new BizzException("圈子升级前-区域范围为空.");
		  }
		  if(upgradeApply.getToZoneLevel()==null){
			  throw new BizzException("圈子升级后-区域范围为空.");
		  }
		  if(upgradeApply.getFromZoneLevel()!=CircleZoneLevelDictEnum.MULTI_COMMUNITY.getCode()){
			  if(upgradeApply.getFromZoneLevel()==upgradeApply.getToZoneLevel()){
				  throw new BizzException("升级前后【区域范围】不能一样.");
			  }
		  }
		  if(StringUtil.isBlank(upgradeApply.getApplyContent())){
			  throw new BizzException("请填写 申请文字说明");
		  }
		  upgradeApply.setStatus(CircleUpgradeApplyStatusDictEnum.TODO.getCode());
		
		return circleUpgradeApplyDao.insert(upgradeApply);
	}

	@Override
	public int auditPass(Integer applyId)  throws BizzException{
		CircleUpgradeApply upgradeApply = circleUpgradeApplyDao.getById(applyId);
		if(upgradeApply==null){
			throw new BizzException("没有找到你的申请信息");	
		}
		CircleBase circleOld = circleBaseDao.getById(upgradeApply.getCircleId());
		if(circleOld == null){
			throw new BizzException("该圈子不存在");
		}
		
		CircleBase circle4update =new CircleBase();
		circle4update.setId(upgradeApply.getCircleId());
		circle4update.setZoneLevel(upgradeApply.getToZoneLevel());//升级后-区域范围
		if(upgradeApply.getToCityId()!=null && upgradeApply.getToZoneLevel() == CircleZoneLevelDictEnum.CITY.getCode()){
			circle4update.setCityId(upgradeApply.getToCityId());
		}
		if(StringUtil.isNotBlank(upgradeApply.getToExtCommunityIds()) && upgradeApply.getToZoneLevel() == CircleZoneLevelDictEnum.MULTI_COMMUNITY.getCode()){
			String newExtCommunityIds = upgradeApply.getToExtCommunityIds();
			if(StringUtil.isNotBlank(circleOld.getExtCommunityIds())){
				//如果以前也扩展了部份社区，现在平级扩展更多社区，那么把更多社区id附加在原有社区ids之后
				newExtCommunityIds = circleOld.getExtCommunityIds() +";"+newExtCommunityIds;
			}
			circle4update.setExtCommunityIds(newExtCommunityIds);
		}
		int updateCount = circleBaseDao.updateById(circle4update);
		if(updateCount==1){
			CircleUpgradeApply apply4update = new CircleUpgradeApply();
			apply4update.setId(applyId);
			apply4update.setStatus(CircleUpgradeApplyStatusDictEnum.AUDITPASS.getCode());
			apply4update.setStatusAt(TimeUtil.now());
			apply4update.setAuditContent("同意升级");
			return circleUpgradeApplyDao.updateById(apply4update);
		}
		return 0;
	}

	@Override
	public int auditNoPass(Integer applyId,String reason)  throws BizzException{
		CircleUpgradeApply upgradeApply = new CircleUpgradeApply();
		upgradeApply = circleUpgradeApplyDao.getById(applyId);
		if(upgradeApply==null){
			throw new BizzException("没有找到你的申请信息");	
		}
		CircleUpgradeApply apply4update = new CircleUpgradeApply();
		apply4update.setId(applyId);
		apply4update.setStatus(CircleUpgradeApplyStatusDictEnum.AUDITNOPASS.getCode());
		apply4update.setStatusAt(TimeUtil.now());
		apply4update.setAuditContent(reason);
		return circleUpgradeApplyDao.updateById(apply4update);
	}
	
}